<template>
  <n-modal v-model:show="isVisible" title="添加供货商" preset="dialog" @close="onClose" @update:show="onClose" style="height: 335px;width: 700px">
    <n-form @submit.prevent="handleSubmit" label-width="120px" class="form-container">
      <div class="form-row" style="margin-top: 20px">
        <n-form-item label="供货商名称：" path="productId">
          <n-input v-model:value="form.name" placeholder="请输入供货商名称" style="width: 300px"/>
        </n-form-item>
        <n-form-item label="供货商地址：" path="quantity" style="margin-left: 20px">
          <n-input v-model:value="form.address" placeholder="请输入地址" style="width: 300px"/>
        </n-form-item>
      </div>

      <div class="form-row">
        <n-form-item label="联系人：" path="contactName">
          <n-input v-model:value="form.contactName" placeholder="请输入联系人姓名" style="width: 300px"/>
        </n-form-item>
        <n-form-item label="联系人电话：" path="planTime" style="margin-left: 20px">
          <n-input v-model:value="form.phone" placeholder="请输入联系人电话" style="width: 300px"/>
        </n-form-item>
      </div>

      <!-- 提交按钮 -->
      <div class="submit-row">
        <n-form-item>
          <n-button type="primary" native-type="submit" @click="handleSubmit">提交</n-button>
        </n-form-item>
      </div>
    </n-form>
  </n-modal>
</template>

<script setup>
  import {ref, watch} from 'vue';
  import {useUserStore} from "@/store/index.js";
  import api from '@/api/admin'
  import {List} from "echarts";
  const userStore = useUserStore()
  const id = userStore.getId();

  const emit = defineEmits(['updatePage'])
  const props = defineProps({
    show: {
      type: Boolean,
      required: true,
    },
    onClose: {
      type: Function,
      required: true,
    },
    option: {
      type: List,
      require: true
    }
  });
  const isVisible = ref(props.show);
  watch(() => props.show, (newVal) => {
    isVisible.value = newVal;
  });

  const form = ref({
    name:'',
    contactName:'',
    phone:'',
    address:'',
  });

  const handleSubmit = async () => {
    try {
      const res = await api.addSupplier(
        {
          ...form.value,
          adminId:id,
        }
      )
      if(res.code === 200) {
        emit("updatePage")
      }
      else {
        $message.error(res.message)
      }
    } catch (err) {
      $message.error("添加供应商失败！")
    }finally{
      clear()
      props.onClose();
    }
  };
  const clear = () => {
    form.value.phone="";
      form.value.address='';
      form.value.name='';
      form.value.contactName='';
  }
</script>
<style scoped>
    .form-container {
        display: flex;
        flex-direction: column;
        width: 100%;
        position: relative;
    }

    .form-row {
        display: flex;
        align-items: center;
        width: 100%;
    }

    .form-row > n-form-item {
        flex: 1;
    }

    .submit-row {
        justify-content: center;
        display: flex;
        width: 100%;
    }
</style>
